package frsf.cidisi.exercise.tp.search.actions;

import frsf.cidisi.exercise.tp.search.*;
import frsf.cidisi.faia.agent.search.SearchAction;
import frsf.cidisi.faia.agent.search.SearchBasedAgentState;
import frsf.cidisi.faia.state.AgentState;
import frsf.cidisi.faia.state.EnvironmentState;

public class ViajarA_Vinizaile extends SearchAction {

    /**
     * This method updates a tree node state when the search process is running.
     * It does not updates the real world state.
     */
    @Override
    public SearchBasedAgentState execute(SearchBasedAgentState s) {
        Indiana_State agState = (Indiana_State) s;
        
        if( agState.getMapa().getAldea("Vinizaile").getVisitada()<=2 && (agState.getposicion().getNombre().equals("Icaedur")||agState.getposicion().getNombre().equals("Peregary")
        		|| agState.getposicion().getNombre().equals("Bresol")||agState.getposicion().getNombre().equals("Cabe")
        		||agState.getposicion().getNombre().equals("Mixocu")))
        {
       
        	
        	agState.getMapa().getAldea("Vinizaile").setVisitada(agState.getMapa().getAldea("Vinizaile").getVisitada()+1);
        	agState.setposicion(agState.getMapa().getAldea("Vinizaile"));
        }
        else {
        	return null;
        }
        return agState;
    }

    /**
     * This method updates the agent state and the real world state.
     */
    @Override
    public EnvironmentState execute(AgentState ast, EnvironmentState est) {
        Isla_Emicere_State environmentState = (Isla_Emicere_State) est;
        Indiana_State agState = ((Indiana_State) ast);

        // TODO: Use this conditions
        // PreConditions: null
        // PostConditions: null
        
        if((agState.getposicion().getNombre().equals("Icaedur")||agState.getposicion().getNombre().equals("Peregary")
        		||  agState.getposicion().getNombre().equals("Bresol")||agState.getposicion().getNombre().equals("Cabe")
        		||agState.getposicion().getNombre().equals("Mixocu"))&& environmentState.getworld().getAldea("Vinizaile").getVisitada()<=2)
        {
        	environmentState.ejecutarPirata();

        	environmentState.getworld().getAldea("Vinizaile").setVisitada(environmentState.getworld().getAldea("Vinizaile").getVisitada()+1);
        	// Update the agent state
        	((Indiana_State) agState).setposicion(environmentState.getworld().getAldea("Vinizaile"));
        	
        	
        	// Update the real world
        	environmentState.setpos_agente(agState.getposicion());
            
        	return environmentState;
        }

        return null;
    }

    /**
     * This method returns the action cost.
     */
    @Override
    public Double getCost() {
        return new Double(1);
    }

    /**
     * This method is not important for a search based agent, but is essensial
     * when creating a calculus based one.
     */
    @Override
    public String toString() {
        return "Viajar A Vinizaile";
    }
}